<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工序编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入工序编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入工序名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序要求" prop="request">
        <el-input
          v-model="queryParams.request"
          placeholder="请输入工序要求"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺文件" prop="document">
        <el-input
          v-model="queryParams.document"
          placeholder="请输入工艺文件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序指导书" prop="guidBook">
        <el-input
          v-model="queryParams.guidBook"
          placeholder="请输入工序指导书"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="启用" prop="enable">
        <el-select v-model="queryParams.enable" placeholder="请输入启用" clearable @keyup.enter.native="handleQuery">
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ruoyiyinzheng:process:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyiyinzheng:process:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyiyinzheng:process:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ruoyiyinzheng:process:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="processList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="工序编号" align="center" prop="code" />
      <el-table-column label="工序名称" align="center" prop="name" />
      <el-table-column label="工序要求" align="center" prop="request" />
      <el-table-column label="工艺文件" align="center" prop="stDocument" />
      <el-table-column label="工序指导书" align="center" prop="stGuidBook" />
      <el-table-column label="设备" align="center" prop="stDeviceName" />
      <el-table-column label="启用" align="center" prop="enable" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enable"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['ruoyiyinzheng:process:edit', 'system:oss:download']">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">工序</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleAddProcessTable" icon="el-icon-key"
              >上传</el-dropdown-item>
              <el-dropdown-item command="handleDownload" icon="el-icon-circle-check"
                                v-hasPermi="['system:oss:download']">下载</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['ruoyiyinzheng:process:edit', 'system:oss:download']">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">指导</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleAddProcessGuidTable" icon="el-icon-key"
              >上传</el-dropdown-item>
              <el-dropdown-item command="handleDownProcessGuidload" icon="el-icon-circle-check"
                                v-hasPermi="['system:oss:download']">下载</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyiyinzheng:process:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyiyinzheng:process:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工序对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工序编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入工序编号" />
        </el-form-item>
        <el-form-item label="工序名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入工序名称" />
        </el-form-item>
        <el-form-item label="工序要求" prop="request">
          <el-input v-model="form.request" placeholder="请输入工序要求" />
        </el-form-item>
        <el-form-item label="工艺文件" prop="document">
          <el-input v-model="form.document" placeholder="请输入工艺文件" />
        </el-form-item>
        <el-form-item label="工序指导书" prop="guidBook">
          <el-input v-model="form.guidBook" placeholder="请输入工序指导书" />
        </el-form-item>
<!--        <el-form-item label="设备" prop="deviceName">-->
<!--          <el-input v-model="form.deviceName" placeholder="请输入设备" />-->
<!--        </el-form-item>-->
        <el-form-item label="设备" prop="deviceName">
          <el-select v-model="form.deviceName" placeholder="请输入设备">
            <el-option
              v-for="d in deviceList"
              :key="d.id"
              :label="d.name"
              :value="d.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="启用" prop="enable">
          <el-select v-model="form.enable" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改OSS对象存储对话框 -->
    <el-dialog :title="title" :visible.sync="openFile" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件名">
          <fileUpload v-model="form.file" @fileUploadSuccess="updateFileSuccess" v-if="type === 0"/>
          <imageUpload v-model="form.file" v-if="type === 1"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProcess, getProcess, delProcess, addProcess, updateProcess } from "@/api/ruoyiyinzheng/process";
import {listDevice} from "@/api/ruoyiyinzheng/device";

export default {
  dicts: ['sys_yes_no'],
  name: "Process",
  data() {
    return {
      deviceList:[],
      openWindowMark:0,
      type:0,
      openFile:false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工序表格数据
      processList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: undefined,
        name: undefined,
        request: undefined,
        document: undefined,
        guidBook: undefined,
        deviceName: undefined,
        enable: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "工序编号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "工序名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDeviceList();
  },
  methods: {
    getDeviceList()
    {
      listDevice().then(response => {
        this.deviceList =  response.rows.map(item => ({
          id: item.id,
          name: item.name,
        }));
      });
    },
    /** 查询工艺列表 */
    updateFileSuccess(data)
    {
      if(this.openWindowMark==1)
      {
        this.form.document=data.ossId;
      }else if(this.openWindowMark==2)
      {
        this.form.guidBook=data.ossId;
      }
      this.openWindowMark=0;
      console.log(data);
    }, handleCommand(command, row) {
      const id = row.id || this.ids
      getProcess(id).then(response => {
        this.form = response.data;
      });
      switch (command) {
        case "handleAddProcessTable":
          this.handleAddProcessTable(row);
          break;
        case "handleDownload":
          this.handleDownload(row);
          break;
        case "handleDownProcessGuidload":
          this.handleDownProcessGuidload(row);
          break;
        case "handleAddProcessGuidTable":
          this.handleAddProcessGuidTable(row);
          break;
        default:
          break;
      }
    },
    handleDownProcessGuidload(row) {
      this.$download.oss(row.guidBook);
    },
    handleAddProcessGuidTable(row)
    {
      this.openWindowMark=2;
      this.openFile=true;
    },
    /** 下载按钮操作 */
    handleDownload(row) {
      this.$download.oss(row.document)
    },
    handleAddProcessTable(row)
    {
      this.openWindowMark=1;
      this.openFile=true;
    },
    /** 查询工序列表 */
    getList() {
      this.loading = true;
      listProcess(this.queryParams).then(response => {
        this.processList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        code: undefined,
        name: undefined,
        request: undefined,
        document: undefined,
        guidBook: undefined,
        deviceName: undefined,
        enable: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工序";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getProcess(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改工序";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateProcess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addProcess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工序编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delProcess(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ruoyiyinzheng/process/export', {
        ...this.queryParams
      }, `process_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
